using System;
using System.Collections.Generic;
using System.Text;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace Acms.AcmsControls.ContentControls.FrontEnd
{
    /// <summary>
    /// Renders image tag for Acms Image stored in DB
    /// </summary>
    public class AcmsImage : WebControl
    {
        /// <summary>
        /// File Id for the Acms Image file
        /// </summary>
        public int? FileId { get; set; }
        /// <summary>
        /// Height to render the image
        /// </summary>
        public int RenderHeight { get; set; }
        /// <summary>
        /// Width to render the image
        /// </summary>
        public int RenderWidth { get; set; }
        /// <summary>
        /// javascript function to call when clicked
        /// </summary>
        public string OnClientClick { get; set; }
        public string ImageUrl { get; private set; }
        /// <summary>
        /// Alt Text Tag
        /// </summary>
        public string AlternateText { get; set; }

        protected override void Render(System.Web.UI.HtmlTextWriter writer)
        {
            if (FileId != null && FileId.Value != 0)
            {
                this.ImageUrl = string.Format("~/File.aspx?Id={0}&width={1}&height={2}", FileId.Value, RenderWidth, RenderHeight);
                base.Render(writer);
                //RenderContents(writer);

            }
        }

        protected override void RenderContents(HtmlTextWriter writer)
        {
            writer.AddAttribute(HtmlTextWriterAttribute.Src, Page.ResolveUrl(ImageUrl));
            writer.AddAttribute(HtmlTextWriterAttribute.Alt, AlternateText);
            writer.RenderBeginTag(HtmlTextWriterTag.Img);
            writer.RenderEndTag();
        }

        protected override void AddAttributesToRender(HtmlTextWriter writer)
        {
            if (!string.IsNullOrEmpty(OnClientClick))
            {
                writer.AddAttribute(HtmlTextWriterAttribute.Onclick, OnClientClick);
            }
            base.AddAttributesToRender(writer);
        }

        protected override object SaveViewState()
        {
            ViewState["FileId"] = FileId;
            ViewState["RenderWidth"] = RenderWidth;
            ViewState["RenderHeight"] = RenderHeight;
            ViewState["OnClientClick"] = OnClientClick;
            ViewState["AlternateText"] = AlternateText;

            return base.SaveViewState();
        }

        protected override void LoadViewState(object savedState)
        {
            base.LoadViewState(savedState);
            if (ViewState["FileId"] is int?)
            {
                FileId = (int?)ViewState["FileId"];
            }
            if (ViewState["RenderWidth"] is int)
            {
                RenderWidth = (int)ViewState["RenderWidth"];
            }
            if (ViewState["RenderHeight"] is int)
            {
                RenderHeight = (int)ViewState["RenderHeight"];
            }
            OnClientClick = ViewState["OnClientClick"] as string;
            AlternateText = ViewState["AlternateText"] as string;

        }
    }
}
